package top.tagao.study00demo.study03demo;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.antherd.smcrypto.sm2.Sm2;
import org.apache.tomcat.util.codec.binary.Base64;
import org.bouncycastle.pqc.math.linearalgebra.IntUtils;

import java.security.KeyPair;

/**
 * @author Viktor.Liu
 * @Date 2023/9/14 12:19
 */

public class SmUtilTest {

    public static void main(String[] args) {

        String txnSt = "01";
        // TODO hbOpenJfService.qryTxnSt(record.getSn());

        Integer sendStatus = Integer.parseInt(txnSt);
        System.out.println(sendStatus);
        //test1();
        loginTest();
//        test3();
//        test4();
//        test5();
//        test6();
    }

    public static void test1() {
        System.out.println("======== SM2使用随机生成的密钥对加密或解密，开始 ========");

        String text = "SM2使用随机生成的密钥对加密或解密";
        SM2 sm2 = SmUtil.sm2();
        // 公钥加密，私钥解密
        String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
        System.out.println("密文：" + encryptStr);
        String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
        System.out.println("明文：" + decryptStr);

        System.out.println("======== SM2使用随机生成的密钥对加密或解密，结束 ========");
    }

    public static void test2() {
        System.out.println("======== SM2使用自定义密钥对加密或解密，开始 ========");

        String text = "2000";

        KeyPair pair = SecureUtil.generateKeyPair("SM2");
        byte[] privateKey = pair.getPrivate().getEncoded();
        //String s = Base64.encodeBase64String(privateKey);
        //System.out.println(Arrays.toString(Base64.decodeBase64(s)));
        System.out.println("私钥："+ Base64.encodeBase64String(privateKey));
        byte[] publicKey = pair.getPublic().getEncoded();
        System.out.println("公钥："+ Base64.encodeBase64String(publicKey));
       String sm2Private = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg7bThQl7AvwmFH2wQPFHnLXRM74jpi4jHVrVc0/HA676gCgYIKoEcz1UBgi2hRANCAASIa/EPC4S0Wv79N5mpn3q2sjU6gqoBqCmPdVx8OkDP/ELj59xX/EeqcjEsM42LXTlr3gwt+tdpvmTtOCp5YxTE";
       String  sm2Public2 = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEiGvxDwuEtFr+/TeZqZ96trI1OoKqAagpj3VcfDpAz/xC4+fcV/xHqnIxLDONi105a94MLfrXab5k7TgqeWMUxA==";

        SM2 sm2 = SmUtil.sm2(sm2Private, sm2Public2);
        // 公钥加密，私钥解密
        String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
        System.out.println("密文：" + encryptStr);
        String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
        System.out.println("明文：" + decryptStr);

        System.out.println("======== SM2使用随机生成的密钥对加密或解密，结束 ========");
    }
    public static void test22() {
        SM2 sm2 = SmUtil.sm2("MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg7bThQl7AvwmFH2wQPFHnLXRM74jpi4jHVrVc0/HA676gCgYIKoEcz1UBgi2hRANCAASIa/EPC4S0Wv79N5mpn3q2sjU6gqoBqCmPdVx8OkDP/ELj59xX/EeqcjEsM42LXTlr3gwt+tdpvmTtOCp5YxTE",null);

        String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd("04234434C884EEBF5F2BBE4BB7DF81F2BBD48C70FEEC2DB440003D9DD57281445B2E8F79D2D77B5C2607C5830669F1246E5D90C3CEE8BCD4A7F9B4CCB23847E10E61FDCA89DD552950223413DEA101832D3932002EEEBCE2BC488D2A108CC3E9E3FBE459225FF4984903C647671248A6F4A7A578C96242885EDFD6ADF245E3ED052A36385DEE4291B38DFBFE96189BFCD", KeyType.PrivateKey));
        System.out.println("明文：" + decryptStr);
    }


    public static void loginTest() {

        String s = Sm2.doEncrypt("{\"point\":490,\"taskId\":6,\"date\":\"2023-09-28\"}", "0440efc27ac0ebd2d0ac97bc9cbb3e1e9dddded0980ef66598f173d758b4f43e44da80381252c9b75ebc7c641135db72967eaac9b26bf88be63893ccd697adff4e");
        System.out.println(s);
        String encryptData = "0866723d3b249037ff3c4f43445944c7c12cd62eb3d15aadb367a50b9571b94ca865e3ec70ba4c90822d773458ed5352464e99891d082c48dc198e889d6e787880c48022c34ac3879240b83572d696525d16165140df8961a6d811866b923293c5ce9439b67038109a40ab3c43c2073cf75ad584d119ae7b8850070362759d0fb5d536f50af13411410047dc7d";
        String decryptData = Sm2.doDecrypt(encryptData, "002cbf5eb528676c9b9b9c5278cb1676cd664aac490ae0e71000f49c96c8c4d2"); // 解密结果
        System.out.println(decryptData);
    }


    public static void test3() {
        System.out.println("======== SM2签名和验签，开始 ========");

        String content = "SM2签名和验签";
        final SM2 sm2 = SmUtil.sm2();
        String sign = sm2.signHex(HexUtil.encodeHexStr(content));
        System.out.println("sign:" + sign);

        boolean verify = sm2.verifyHex(HexUtil.encodeHexStr(content), sign);
        System.out.println("校验结果为：" + verify);

        System.out.println("======== SM2签名和验签，结束 ========");
    }

    public static void test4() {
        System.out.println("======== 摘要加密算法SM3，开始 ========");

        String text = "摘要加密算法SM3";
        System.out.println("text:" + text);
        String digestHex = SmUtil.sm3(text);
        System.out.println("digestHex：" + digestHex);

        System.out.println("======== 摘要加密算法SM3，结束 ========");
    }

    public static void test5() {
        System.out.println("======== 对称加密SM4，开始 ========");

        String content = "对称加密SM4";
        SymmetricCrypto sm4 = SmUtil.sm4();

        String encryptHex = sm4.encryptHex(content);
        System.out.println("密文：" + encryptHex);
        String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
        System.out.println("明文：" + decryptStr);

        System.out.println("======== 对称加密SM4，结束 ========");
    }

    /***
     * 分开的：私钥签名，公钥验签
     */
    public static void test6(){
        System.out.println("======== 私钥签名公钥验签，开始 ========");
        String txt = "私钥签名公钥验签";

        String privateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgfVNnk3eKogEiEAvYinFGZev31dT4gQqcEAAidzIKhC2gCgYIKoEcz1UBgi2hRANCAATolVEVuON8S9aOpnogLTzXzo0Dx58LMFjex7XPPcPtSmuq1Rh/ZM2qbYdZyzdnca8eCeR+cg+44rb/TyPRlH23";
        String publicKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE6JVRFbjjfEvWjqZ6IC08186NA8efCzBY3se1zz3D7UprqtUYf2TNqm2HWcs3Z3GvHgnkfnIPuOK2/08j0ZR9tw==";

        SM2 sm2Sign = SmUtil.sm2(privateKey, null);
        byte[] sign = sm2Sign.sign(txt.getBytes(), null);
        System.out.println("sign:" + Base64.encodeBase64String(sign));

        SM2 sm2 = SmUtil.sm2(null, publicKey);
        boolean verify = sm2.verify(txt.getBytes(), sign);
        System.out.print("verify:" + verify);
        System.out.println("======== 私钥签名公钥验签，结束 ========");
    }
}


